export default {
  // 日期格式转换--时间字符串转化成视觉格式
  // Add by zgf, Updated on 2018/9/4
  // 使用示例：this.$util.formatDate('/Date(1435646188067+0800)/','yyyy-MM-dd hh:mm:ss')
  // 使用示例：this.$util.formatDate('2018-12-12T12:12:12','yyyy-MM-dd hh:mm:ss')
  // 使用示例：this.$util.formatDate('2018-12-12 12:12:12','yyyy/MM/dd hh:mm:ss')
  // 使用示例：this.$util.formatDate(new Date(),'yyyy/MM/dd hh:mm:ss')
  // 作为方法调用：this.$util.formatDate(时间,[格式])
  // 作为filter调用：{{时间 | formatDate([格式])}}
  formatDate: function (v, format) {
    if (!v) return "";
    var d = v;
    if (typeof v === 'string') {
      if (v.indexOf("/Date(") > -1)
        d = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
      else
        d = new Date(Date.parse(v.replace(/-/g, "/").replace("T", " ").split(".")[0])); //.split(".")[0] 用来处理出现毫秒的情况，截取掉.xxx，否则会出错
    }
    var o = {
      "M+": d.getMonth() + 1,
      "d+": d.getDate(),
      "h+": d.getHours(),
      "m+": d.getMinutes(),
      "s+": d.getSeconds(),
      "q+": Math.floor((d.getMonth() + 3) / 3),
      "S": d.getMilliseconds()
    };
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
      if (new RegExp("(" + k + ")").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
      }
    }
    return format;
  },
  //计算两个时间的时间间隔
  //start 2019-12-12 12:12:12
  //end 2019-12-13 12:12:12
  //returnSeconds返回毫秒数，默认返回xx天xx时xx分
  timeDuration: function (start, end, returnSeconds) {
    start = this.formatDate(start, 'yyyy/MM/dd hh:mm:ss');
    end = this.formatDate(end, 'yyyy/MM/dd hh:mm:ss');
    let startTime = new Date(start); // 开始时间
    let endTime = new Date(end); // 结束时间
    let usedTime = endTime - startTime; // 相差的毫秒数
    if (returnSeconds) return usedTime;
    let days = Math.floor(usedTime / (24 * 3600 * 1000)); // 计算出天数
    let leavel = usedTime % (24 * 3600 * 1000); // 计算天数后剩余的时间
    let hours = Math.floor(leavel / (3600 * 1000)); // 计算剩余的小时数
    let leavel2 = leavel % (3600 * 1000); // 计算剩余小时后剩余的毫秒数
    let minutes = Math.floor(leavel2 / (60 * 1000)); // 计算剩余的分钟数
    return days + '天' + hours + '时' + minutes + '分';
  },
  //判断某年是否是闰年
  //2月平年28天，闰年29天
  isLeapYear(Year) {
    if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
      return (true);
    } else { return (false); }
  },
  // 根据日期算出是第几周
  getWeekByDate(dt) {
    dt = this.formatDate(dt, 'yyyy/MM/dd')
    let d1 = new Date(dt);
    let d2 = new Date(dt);
    let d2Year = d2.getFullYear();
    let weekNum = new Date(d2Year + '/1/1').getDay();
    let firsrtMonday = d2Year + '/1/1'
    if(weekNum!==1){
      firsrtMonday = d2Year + '/1/' + (7 - weekNum + 2)
    }

    d2 = new Date(firsrtMonday);
    let rq = d1 - d2;
    let days = Math.ceil(rq / (24 * 60 * 60 * 1000));
    days++
    let num = Math.ceil(days / 7);
    if (+num == 0) {
      if(days===-6){
        return [d2Year, 1]
      }
      var d3 = new Date((d2Year - 1) + '/12/31')
      return [d2Year - 1, this.getWeekByDate(d3)[1]]
    }
    return [d2Year, num];
  },

  //通过第几周计算这周的开始时间和结束时间
  //参数 [2019,47]
  getStartAndEndDateByWeekNum:function(weekNum){
    if(weekNum.length<2){
      weekNum=[2019,47]
    }
    let addDays = (+weekNum[1]-1)*7
    let d2Year = weekNum[0];
    let weekNum1 = new Date(d2Year + '/1/1').getDay();
    let firsrtMonday = d2Year + '/1/1'
    if(weekNum1!==1){
      firsrtMonday = d2Year + '/1/' + (7 - weekNum1 + 2)
    }
    let firsrtMondayObj = new Date(firsrtMonday);
    let startdate = firsrtMondayObj.setDate(firsrtMondayObj.getDate()+addDays)
    let endDate = firsrtMondayObj.setDate(new Date(startdate).getDate() + 6)
    return {
      startDate:this.formatDate(new Date(startdate),'yyyy/MM/dd'),
      endDate:this.formatDate(new Date(endDate),'yyyy/MM/dd')
    };
  },
  //通过月换算成月的开始日期和结束日期
  //monthValue [2019,12] 2019年12月
  getStartAndEndDateByMonth(monthValue){
    if(monthValue.length<2){
      return {
        startDate:'',
        endDate:''
      }
    }
    let month2 = this.isLeapYear(monthValue[0]) ? 29 : 28 //2月平年28天，闰年29天
    let lastDayArr = [31, month2, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    let year = monthValue[0]
    let month = monthValue[1]
    return {
      startDate:year+'/'+month+'/'+'1',
      endDate:year+'/'+month+'/'+lastDayArr[month-1],
    }
  },

  //获取某元素所在的滚动元素
  getScrollEventTarget(element, rootParent = window){
    let node = element;
    while (
      node &&
      node.tagName !== 'HTML' &&
      node.tagName !== 'BODY' &&
      node.nodeType === 1 &&
      node !== rootParent
    ) {
      const { overflowY } = window.getComputedStyle(node);
      if (overflowY === 'scroll' || overflowY === 'auto') {
        return node;
      }
      node = node.parentNode;
    }
    return rootParent;
  }
}